back = imread('background19.jpg');
fore = imread('foreground0.jpg');
figure;imshow(back);
figure;imshow(fore);

 result = back(:,:,:) - fore(:,:,:);
  result = imsubtract(result,20);
 result = NegPic(result);
 result = im2bw(result, 0.5);



figure; imshow(result); title('Output');
RCol=zeros(480,3);
fore2 = rgb2gray(fore);



for i=1:480
    for j=1:640 
        if result(i,j)==1
            fore2(i,j)=255;           
        end
    end
end

a=1;


for i=1:480
    for j=1:640 
            if fore2(i,j)~=255
                RCol(a,1)=i;
                RCol(a,2)=j;
                break;
            end    
    end
    for k=640:-1:1
         if fore2(i,k)~=255
                RCol(a,3)=k;
                break;
         end
    end
    a=a+1;
end


for i=1:480
for j=1:640
    if j<RCol(i,2)||j>RCol(i,3) 
    fore(i,j,1)=255;
    fore(i,j,2)=255;
   fore(i,j,3)=255;
end
end

end



figure;imshow(fore);



